var applyNos=sessionStorage.getItem("applyNos")
var afterSalesdetailid=""
var refundSorceall=""//类型
/*通过订单号获取订单详情*/
getOrderDetail()
function getOrderDetail() {
    $.ajax({
        type: "POST",
        url: root.serviceProviderManagement.getReverseOrderDetail,
        cache: false, //禁用缓存
        dataType: "json",
        async: false,
        data: {
            "refundId": applyNos, // 订单编号
        },
        success: function (results) {
            var datas=results.data
            //详情
           /* 基本信息*/
            var orderState=changeOrderState(datas.refundStatus)
            $("#orderState").html(orderState)
            if(datas.refundStatus==10){
                $("#detailCancel").css({
                    display: "inline"
                });
            }else {
                $("#detailCancel").css({
                    display: "none"
                });
            }
            $("#applyNo").html(datas.applyNo)
            $("#orderId").html(datas.orderId)
            afterSalesdetailid=datas.orderId
            var refundSorce=refundSorces(datas.refundSorce)
            $("#refundSorce").html(refundSorce)
            refundSorceall=datas.refundSorce
            //查询退货凭证是否展示
            queryInformation()
            $("#customerName").html(datas.customerName)
            $("#salesManagerName").html(datas.salesManagerName)
            $("#baseInvoiceType").html(datas.baseInvoiceType)
            /*操作信息*/
            $("#operatorTime").html(datas.operatorTime)
            $("#operator").html(datas.operator)
            $("#auditInfo").html(datas.auditInfo)
            /*退货申请信息*/
            var refundRole=""
            if(datas.refundRole==10){
                refundRole="买家"
            }else if(datas.refundRole==20){
                refundRole="客服"
            }
            $("#refundRole").html(refundRole)
            $("#refundReason").html(reason(datas.refundReason))
            $("#problemRemark").html(datas.problemRemark)
            $("#receiveGoodsAddress").html(datas.receiveGoodsAddress)
            $("#applyDate").html(datas.applyDate)
            $("#applyOperator").html(datas.applyOperator)
            $("#orderProof").html(datas.orderProof)
            var pickupMethods=""
            if(datas.pickupMethod==10){
                pickupMethods="商家上门"
            }else  if(datas.pickupMethod==20){
                pickupMethods="客户邮寄"
            }else  if(datas.pickupMethod==30){
                pickupMethods="门店退货"
            }
            $("#pickupMethod").html(pickupMethods)
            $("#beforeReturnReason").html(reason2(datas.beforeReturnReason))
            /*退款信息*/
            if(datas.refundProductAmount!=null&&datas.refundProductAmount!=""){
                $("#refundProductAmount").html((datas.refundProductAmount).toFixed(2))
            }else {
                $("#refundProductAmount").html('0.00')
            }
            if(datas.returnShippingFee!=null&&datas.returnShippingFee!=""){
                $("#returnShippingFee").html((datas.returnShippingFee).toFixed(2))
            }else {
                $("#returnShippingFee").html('0.00')
            }
            if(datas.couponShippingAmount!=null&&datas.couponShippingAmount!=""){
                $("#couponShippingAmount").html((datas.couponShippingAmount).toFixed(2))
            }else {
                $("#couponShippingAmount").html("0.00")
            }

            $("#baseInvoiceType2").html(datas.baseInvoiceType)
            $("#serviceFee").html((datas.serviceFee).toFixed(2))
            if(datas.returnFreightAmount!=null&&datas.returnFreightAmount!=""){
                $("#returnFreightAmount").html((datas.returnFreightAmount).toFixed(2))
            }else {
                $("#returnFreightAmount").html('0.00')
            }
            if(datas.collectionGoodsFreight!=null&&datas.collectionGoodsFreight!=""){
                $("#collectionGoodsFreight").html((datas.collectionGoodsFreight).toFixed(2))
            }else {
                $("#collectionGoodsFreight").html('0.00')
            }
            if(datas.shouldReturnAmount!=null&&datas.shouldReturnAmount!=""){
                $("#shouldReturnAmount").html((datas.shouldReturnAmount).toFixed(2))
            }else {
                $("#shouldReturnAmount").html('0.00')
            }

            $("#immeReduceShare").html(datas.immeReduceShare)
            $("#integralShareAmount").html(datas.integralShareAmount)
            $("#companyName").html(datas.companyName)
            $("#refundDesc").val(datas.refundDesc)
            /*商品信息*/
            productInformation(datas.itemResponseList)


        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}
//查询退货凭证
function queryInformation() {
    var proofTypes=""
    if (refundSorceall==10) {
        //拒收
        proofTypes=30
    }else if (refundSorceall==11) {
        //拒收-拦截
        proofTypes=20
    }else if (refundSorceall==20) {
        //退货
        proofTypes=40
    }
    $.ajax({
        contentType: "application/json;charset=UTF-8",
        url: root.serviceProviderManagement.listProof,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        data: JSON.stringify({
            "businessId": applyNos, // 编号
            "proofType":proofTypes

        }),
        success: function (results) {
            if(results.data!="" && results.data!=null){

                $("#rejectCheck").css({
                    display: "inline"
                });
            }else {
                $("#rejectCheck").css({
                    display: "none"
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}
/*转换状态函数*/
function changeOrderState(orderState) {
    switch (orderState) {
        /*订单状态*/
        case 10:
            return "待确认";
            break;
        case 50:
            return "已完成";
            break;
        case 60:
            return "已关闭";
            break;
    }
}
/*转换类型函数*/
function refundSorces(orderState) {
    switch (orderState) {
        /*订单状态*/
        case 10:
            return "拒收";
            break;
        case 11:
            return "拒收-拦截";
            break;
        case 20:
            return "退货";
            break;
    }
}

function reason(data) {
    switch(data) {
        /*退货原因*/
        case 0:
            return "";
        case 10:
            return "商品质量问题";
            break;
        case 11:
            return "其它原因";
            break;
        case 12:
            return "收到商品与网站展示不符(颜色、图片、规格、产地及详情描述)";
            break;
        case 13:
            return "客户原因无理由退货";
            break;
        case 14:
            return "资质问题";
            break;
        case 20:
            return "大小/型号与商品描述不符";
            break;
        case 30:
            return "颜色/图案与商品描述不符";
            break;
        case 40:
            return "功能/效果与商品描述不符";
            break;
        case 50:
            return "商城发错货";
            break;
        case 51:
            return "客户订购错误/计划有变";
            break;
        case 52:
            return "价格高";
            break;
        case 53:
            return "无法满足到货时效";
            break;
        case 54:
            return "支付问题";
            break;
        case 55:
            return "重复下单";
            break;
        case 56:
            return "暂时不需要了";
            break;
        case 57:
            return "订单信息有误";
            break;
        case 58:
            return "无货";
            break;
        case 59:
            return "其他";
            break;
        case 60:
            return "少件/漏发";
            break;
        case 61:
            return "商品质量问题";
            break;
        case 62:
            return "价格错误";
            break;
        case 70:
            return "多买/买错不想要了";
            break;
        case 80:
            return "到货物流破损";
            break;
        case 90:
            return "发票问题";
            break;
        case 100:
            return "未按约定时间送货";
            break;
        case 101:
            return "客户原因未收货";
            break;
        case 102:
            return "其它原因";
            break;
        case 103:
            return "现场无人收货";
            break;
        case 104:
            return "工地验收不合格";
            break;
        case 105:
            return "下错订单";
            break;
        case 106:
            return "送错货";
            break;
        case 107:
            return "客户地址留错";
            break;
        case 108:
            return "物流送货延迟";
            break;
        case 140:
            return "资质问题";
            break;
        case 53:
            return "货未集齐";
            break;
    }
}
function reason2(data) {
    switch(data) {
        /*前台退货原因*/
        case 0:
            return "";
        case 10:
            return "商品质量问题";
            break;
        case 20:
            return "大小/型号与商品描述不符";
            break;
        case 30:
            return "颜色/图案与商品描述不符";
            break;
        case 40:
            return "功能/效果与商品描述不符";
            break;
        case 50:
            return "商城发错货";
            break;
        case 60:
            return "商品发错/少件/漏发";
            break;
        case 70:
            return "多买/买错不想要了";
            break;
        case 80:
            return "到货物流破损";
            break;
        case 90:
            return "发票问题";
            break;
        case 100:
            return "未按约定时间送货";
            break;
        case 110:
            return "其它原因";
            break;
        case 120:
            return "收到商品与网站展示不符";
            break;
        case 130:
            return "无理由退货";
            break;
        case 140:
            return "资质问题";
            break;
        case 51:
            return "客户订购错误/计划有变";
            break;
        case 52:
            return "价格高";
            break;
        case 53:
            return "无法满足到货时效";
            break;
        case 54:
            return "支付问题";
            break;
        case 55:
            return "重复下单";
            break;
        case 56:
            return "暂时不需要了";
            break;
        case 57:
            return "订单信息有误";
            break;
        case 58:
            return "无货";
            break;
        case 59:
            return "其他";
            break;
        case 60:
            return "少件/漏发";
            break;
        case 61:
            return "商品质量问题";
            break;
        case 62:
            return "价格错误";
            break;
    }
}

//商品信息
function productInformation(datas) {
    var dataSource = new Array();
//table的数据源
    var table; //table对象
    dataSource = [];
    console.log(datas)
    dataSource.splice(0, dataSource.length); //清空数组
    if(datas!=null && datas!="" ){
        $.each(datas, function(i, field) {
            var rowData = new Array();
            rowData.push(field.productPic);
            rowData.push(field.skuCode);
            rowData.push(field.productName);
            rowData.push(field.skuInfo);
            rowData.push(field.buyerCount);
            rowData.push(field.unitName);
            rowData.push(field.salePrice);
            rowData.push(field.purchasePrice);
            rowData.push(field.refundCount);
            dataSource.push(rowData);
        });
        initDataTable(dataSource);
    }
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": false,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [{
                "visible": true,
                "targets": 0
            },
                {
                    "render": function(data, type, row) {
                        var val = '<div style="cursor:pointer;"><img title="" style="float: left;width: 100px;height: 100px;" name="" src="'+row[0]+'" alt=""><div style="float: left;width:180px;padding-top: 20px;margin-left: 10px;"><div>' + row[1] + '</div><div>' + row[2] + '</div><div>' + row[3] + '</div></div></div>';
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function(data, type, row) {
                        var val = '<span>' + row[4] + '' + row[5] + '</span>';
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[6]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },

                {
                    "render": function(data, type, row) {
                        var val = '<span>' + row[8] + '' + row[5] + '</span>';
                        return val;
                    },
                    "targets": 4
                },

            ]
        });
    };
}
//查看订单
function checkOrder(title, url) {
    sessionStorage.setItem("serviceProviderorderid", afterSalesdetailid);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//取消订单
function cancelorder() {
    sessionStorage.setItem("afterSales_detailsorderid",afterSalesdetailid)//订单号
    sessionStorage.setItem("afterSales_detailsapplyNos",applyNos)//订单编号
    layer_show("取消订单", "../afterSales/cancleAlert.html", "600", "300");
}
//查看收货凭证
function receiptCertificate() {
    $("#receiptCertificates").modal("show")
    var proofTypes=""
    if (refundSorceall==10) {
        //拒收
        proofTypes=30
    }else if (refundSorceall==11) {
        //拒收-拦截
        proofTypes=20
    }else if (refundSorceall==20) {
        //退货
        proofTypes=40
    }

    $.ajax({
        contentType: "application/json;charset=UTF-8",
        url: root.serviceProviderManagement.listProof,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        data: JSON.stringify({
            "businessId": applyNos, //编号
            "proofType":proofTypes

        }),
        success: function (results) {
            var proofUrlsall= results.data[0].proofUrls.split(",");
            var htmls=''
            for (var i=0;i<proofUrlsall.length;i++) {
                htmls+='<img src="'+proofUrlsall[i]+'" class="ml-10" width="80px" height="80px">'

            }
            $("#proofUrls").html(htmls)
            $("#proofRemark").html(results.data[0].proofRemark)
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}
//查看收货凭证关闭
function closereceiptCertificates() {
    $("#receiptCertificates").modal("hide")
}